import { Vector } from "./math";

export class MassPoint {
    constructor(
        public mass: number,
        public p: Vector,
        public v: Vector = Vector.Zero,
    ) {
        if (mass <= 0) throw new Error('mass <= 0');
    }
    public impluse(i: Vector) {
        // console.log(i);
        this.v = this.v.add(i.scale(1 / this.mass));
    }
    public move(dt: number) { this.p = this.p.add(this.v.scale(dt)); }
}

export class Spring {
    constructor(
        public a: MassPoint,
        public b: MassPoint,
        public k: number,
        public x0: number,
    ) { }
    public act(dt: number) {
        const asb = this.a.p.sub(this.b.p);
        const ra = asb.idfy();
        const len = asb.length();
        const f = ra.scale(this.x0 - len);
        this.a.impluse(f.scale(dt));
        this.b.impluse(f.scale(-dt));
    }
}
